/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.web.controller;

import com.boc.botv.common.Database;
import com.boc.botv.model.User;
import com.boc.botv.service.UserManager;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Fabien Renaud
 */
@Controller
@RequestMapping("/")
public class HomeController {
  @Autowired
  private UserManager manager;
  
  private Map<String, Object> map = new HashMap<String, Object>();

  public HomeController() {
    map.put("menu", "home");
    map.put("path", "");
  }

  @RequestMapping("index")
  public void indexHandler(Model model) {
    model.addAllAttributes(map);
    model.addAttribute("title", "BetonTv : Paris sportifs, sur la TV et l'actualité.");
    model.addAttribute("currentUrl", "/index.htm");
  }

  @RequestMapping("contact")
  public void contactHandler(Model model) {
    model.addAllAttributes(map);
    model.addAttribute("title", "Contacter les webmestres BetOnTV");
    model.addAttribute("currentUrl", "/contact.htm");
  }

  @RequestMapping("lost-password")
  public void lostPasswordHandler(Model model) {
    model.addAllAttributes(map);
    model.addAttribute("title", "Mot de passe oublié");
    model.addAttribute("currentUrl", "/index.htm");
  }

  @RequestMapping(value = "/lost-password", method = RequestMethod.POST)
  public String lostPasswordHandler(
    @RequestParam(value = "login", required = true) String login,
    @RequestParam(value = "email", required = true) String email) {
    Database.startTransaction(false);
      User user = manager.getUser(login);
      if (user == null) {
        Database.closeTransaction();
        return "lost-password";
      }
      if (!user.getEmail().equals(email)) {
        Database.closeTransaction();
        return "lost-password";
      }
      manager.regeneratePassword(login);
      Database.closeTransaction();
      return "redirect:/index.htm";
  }

  @RequestMapping("cgu")
  public void cguHandler(Model model) {
    model.addAllAttributes(map);
    model.addAttribute("title", "Conditions Générales d'Utilisation de BetonTV");
    model.addAttribute("currentUrl", "/cgu.htm");
  }

  @RequestMapping("mentions")
  public void mentionsHandler(Model model) {
    model.addAllAttributes(map);
    model.addAttribute("title", "Mentions Légales de BetonTV");
    model.addAttribute("currentUrl", "/mentions.htm");
  }

  @RequestMapping(value="checklogin", method=RequestMethod.POST)
  public String checkLoginHandler(@RequestParam(value="login", required=true) String login, Model model) {
    Database.startTransaction(true);
    boolean r = manager.loginExists(login);
    model.addAttribute("result", r ? "1" : "0");
    Database.closeTransaction();
    return "xml/boolean";
  }

  @RequestMapping(value="checkemail", method=RequestMethod.POST)
  public String checkEmailHandler(@RequestParam(value="email", required=true) String email, Model model) {
    Database.startTransaction(true);
    boolean r = manager.mailExists(email);
    model.addAttribute("result", r ? "1" : "0");
    Database.closeTransaction();
    return "xml/boolean";
  }
}
